package spring.pdfboxmaven;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;

import org.pdfbox.examples.util.PrintTextLocations;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDPage;
import org.pdfbox.pdmodel.font.PDFont;
import org.pdfbox.util.PDFTextStripper;
import org.pdfbox.util.TextPosition;

/**
 * Hello world!
 *
 */
public class App {

    public static void main(String[] args) throws Exception {
        String[] docs = new String[]{"/home/silvia/Escritorio/apuntes_sistemas/uoc/arquitectura_computadoras.pdf"};
        PDDocument dDocument = PDDocument.load(new File("/home/silvia/Escritorio/apuntes_sistemas/uoc/arquitectura_computadoras.pdf"));
        List dPages = dDocument.getDocumentCatalog().getAllPages();


        //PrintDocumentMetaData  documentMetaData= dDocument.getPageFormat(1).getPaper()



        PDFTextStripper dFTextStripper = new PDFTextStripper();
        /*
        PDFText2HTML dFText2HTML = new PDFText2HTML();
        dFText2HTML.setStartPage(1);
        dFText2HTML.setEndPage(3);


        
        StringWriter stringWriter = new StringWriter();
        dFText2HTML.writeText(dDocument, stringWriter);

         *
         */
        //  System.out.println(stringWriter.toString());

        PrintTextLocations printTextLocations = new PrintTextLocations();

        System.out.println("PAG ---------------BEGIN-----------" + dPages.size());
        for (int i = 0; i < dPages.size(); i++) {
            System.out.println("PAG ---------------BEGIN-----------" + i);
            PDPage pDPage = (PDPage) dPages.get(i);
            printTextLocations.processStream(pDPage, pDPage.findResources(), pDPage.getContents().getStream());
            System.out.println("PAG ---------------END----------" + i);
        }



    }
}

class Token {

    String character;
    PDFont font;
    float height;
    float width;
    float sizeFont;
    float widthOfSpace;
    float wordSpacing;
    float x;
    float y;
    float xScale;
    float yScale;

    public Token() {
    }

    public Token(String character, PDFont font, float height, float width, float sizeFont, float widthOfSpace, float wordSpacing, float x, float y, float xScale, float yScale) {
        this.character = character;
        this.font = font;
        this.height = height;
        this.width = width;
        this.sizeFont = sizeFont;
        this.widthOfSpace = widthOfSpace;
        this.wordSpacing = wordSpacing;
        this.x = x;
        this.y = y;
        this.xScale = xScale;
        this.yScale = yScale;
    }

    

    public String getCharacter() {
        return character;
    }

    public void setCharacter(String character) {
        this.character = character;
    }

    public PDFont getFont() {
        return font;
    }

    public void setFont(PDFont font) {
        this.font = font;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    public float getWidth() {
        return width;
    }

    public void setWidth(float width) {
        this.width = width;
    }

    public float getSizeFont() {
        return sizeFont;
    }

    public void setSizeFont(float sizeFont) {
        this.sizeFont = sizeFont;
    }

    public float getWidthOfSpace() {
        return widthOfSpace;
    }

    public void setWidthOfSpace(float widthOfSpace) {
        this.widthOfSpace = widthOfSpace;
    }

    public float getWordSpacing() {
        return wordSpacing;
    }

    public void setWordSpacing(float wordSpacing) {
        this.wordSpacing = wordSpacing;
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getxScale() {
        return xScale;
    }

    public void setxScale(float xScale) {
        this.xScale = xScale;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getyScale() {
        return yScale;
    }

    public void setyScale(float yScale) {
        this.yScale = yScale;
    }

    public String toString() {
        String cachar = getCharacter();
        PDFont dFont = getFont();
        float h = getHeight();
        float w = getWidth();
        float widthOfSpace = getWidthOfSpace();
        float wordSpacing = getWordSpacing();
        float x = getX();
        float y = getY();
        float xScale = getxScale();
        float yScale = getyScale();
        System.out.println("char"+cachar+" h="+h+" w="+w+" widthOfSpace="+widthOfSpace+" wordSpacing="+wordSpacing+ " X="+x);
        return super.toString();
    }
}

class PrintTextLocationsTokens extends PrintTextLocations {

    private List bufferTokens;

    public PrintTextLocationsTokens() throws IOException {
        bufferTokens = new ArrayList();
        super.setSortByPosition(true);
    }

    protected void showCharacter(TextPosition text) {
        String cachar = text.getCharacter();
        PDFont dFont = text.getFont();
        float h = text.getHeight();
        float w = text.getWidth();
        float sizeFont = text.getFontSize();

        float widthOfSpace = text.getWidthOfSpace();
        float wordSpacing = text.getWordSpacing();
        float x = text.getX();
        float y = text.getY();
        float xScale = text.getXScale();
        float yScale = text.getYScale();
        Token token=new Token(cachar, dFont, h, w, sizeFont, widthOfSpace, wordSpacing, x, y, xScale, yScale);
        bufferTokens.add(token);
        
    }

    public List getBufferTokens() {
        return bufferTokens;
    }

   

    
}

